package com.wg.dome.handle;

import com.wg.common.ioc.WgIoc;
import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.entity.config.InitShopEntity;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.HeroEntity;
import com.wg.dome.entity.user.ItemEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.BuyGoodsRequest;
import com.wg.dome.operation.BuyGoodsResponse;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.repository.*;
import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.common.net.SendParameters;
import com.wg.game.IdGenerator;

/**
 * 购买商品
 */
@Request(OperationCode = OperationCode.BUY_GOOD)
public class HandleBuyGoodsRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        BuyGoodsRequest buyGoodsRequest = new BuyGoodsRequest(request);

        if (!buyGoodsRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(buyGoodsRequest);
        }

        if (buyGoodsRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
        }

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = playerRepository.get(buyGoodsRequest.getPlayerId());
        if (playerEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.PLAYER_DATA_ERROR, peer.getPlayerId());
        }

        InitShopRepository initShopRepository = new InitShopRepository();
        InitShopEntity initShopEntity = initShopRepository.get(buyGoodsRequest.getGoodsId());
        if (initShopEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.ITEM_ERROR);
        }

        if (buyGoodsRequest.getCount() < 0 || buyGoodsRequest.getCount() > initShopEntity.getMaxCount()){
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.GOODS_COUNT_ERROR);
        }

        OperationResponse errorResponse = decreaseGold(initShopEntity, playerEntity, buyGoodsRequest);
        if (errorResponse != null){
            return errorResponse;
        }
        playerRepository.update(playerEntity);

        return addItem(initShopEntity, playerEntity, buyGoodsRequest);
    }

    /**
     * 扣钱
     */
    private OperationResponse decreaseGold(InitShopEntity entity, PlayerEntity playerEntity, BuyGoodsRequest buyGoodsRequest){
        long totalPrice = entity.getPrice() * buyGoodsRequest.getCount();
        switch (entity.getGoldType()) {
            case 1:
                if (playerEntity.getGold() < totalPrice) {
                    return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(), ErrorCode.GOLD_LIMIT);
                }
                long money = playerEntity.getGold() - totalPrice;
                playerEntity.setGold(money);
                break;
            case 2:
                if (playerEntity.getRmb() < totalPrice) {
                    return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(), ErrorCode.RMB_LIMIT);
                }
                money = playerEntity.getRmb() - totalPrice;
                playerEntity.setRmb(money);
                break;
            default:
                break;
        }
        return null;
    }

    /**
     * 添加商品到背包
     */
    private OperationResponse addItem(InitShopEntity initShopEntity, PlayerEntity playerEntity, BuyGoodsRequest buyGoodsRequest){
        IdGenerator idGenerator = WgIoc.get(IdGenerator.class);
        if (initShopEntity.getItemType() == 1){
            InitItemRepository initItemRepository = new InitItemRepository();
            if (initItemRepository.get(initShopEntity.getItemId()) == null){
                return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(), ErrorCode.ITEM_ERROR);
            }

            ItemRepository itemRepository = new ItemRepository();
            ItemEntity item = itemRepository.get(buyGoodsRequest.getPlayerId(), itemEntity -> itemEntity.getsId() == initShopEntity.getItemId());
            if (item == null){
                item = new ItemEntity();
                item.setId(WgIoc.get(IdGenerator.class).nextId());
                item.setPlayerId(buyGoodsRequest.getPlayerId());
                item.setsId(initShopEntity.getItemId());
                item.setCount(buyGoodsRequest.getCount());
                itemRepository.add(item);
            }else {
                item.setCount(item.getCount() + buyGoodsRequest.getCount());
                itemRepository.update(item);
            }

            BuyGoodsResponse response = new BuyGoodsResponse();
            response.setGold(playerEntity.getGold());
            response.setRmb(playerEntity.getRmb());
            response.setItemInfo(item.toHashTable());
            response.setItemType(initShopEntity.getItemType());

            return new OperationResponse(buyGoodsRequest.getRequest().getOperationCode(), response);
        }else if (initShopEntity.getItemType() == 2){
            InitEquipRepository initEquipRepository = new InitEquipRepository();
            if (initEquipRepository.get(initShopEntity.getItemId()) == null){
                return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(), ErrorCode.ITEM_ERROR);
            }

            EquipRepository equipRepository = new EquipRepository();
            EquipEntity equipEntity = new EquipEntity();
            equipEntity.setId(idGenerator.nextId());
            equipEntity.setPlayerId(playerEntity.getId());
            equipEntity.setsId(initShopEntity.getItemId());
            equipRepository.add(equipEntity);

            BuyGoodsResponse response = new BuyGoodsResponse();
            response.setGold(playerEntity.getGold());
            response.setRmb(playerEntity.getRmb());
            response.setItemInfo(equipEntity.toHashTable());
            response.setItemType(initShopEntity.getItemType());

            return new OperationResponse(buyGoodsRequest.getRequest().getOperationCode(), response);
        }
        else if (initShopEntity.getItemType() == 3){
            InitHeroRepository initHeroRepository = new InitHeroRepository();
            if (initHeroRepository.get(initShopEntity.getItemId()) == null){
                return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(),  ErrorCode.ITEM_ERROR);
            }

            HeroRepository heroRepository = new HeroRepository();
            HeroEntity heroEntity = new HeroEntity();
            heroEntity.setPlayerId(playerEntity.getId());
            heroEntity.setId(idGenerator.nextId());
            heroEntity.setsId(initShopEntity.getItemId());
            heroEntity.setDjLv((short) 1);
            heroEntity.setFsLv((short) 0);
            heroEntity.setJjLv((short) 0);
            heroEntity.setStarLv((short) 0);
            heroRepository.add(heroEntity);

            BuyGoodsResponse response = new BuyGoodsResponse();
            response.setGold(playerEntity.getGold());
            response.setRmb(playerEntity.getRmb());
            response.setItemInfo(heroEntity.toHashTable());
            response.setItemType(initShopEntity.getItemType());

            return new OperationResponse(buyGoodsRequest.getRequest().getOperationCode(), response);
        }
        else {
            return ErrorHelper.getErrorResponse(buyGoodsRequest.getRequest().getOperationCode(),  ErrorCode.BUY_ITEM_ERROR, initShopEntity.getItemType());
        }
    }
}
